﻿using UnityEngine;
using System.Collections;
using UniLua;
using System;

namespace TCG.FFI
{
	/// <summary>
	/// 将UnityEngine.Color导出给lua。
	/// 
	/// 未实现：
	/// * mul : 未实现 Color * float
	/// 
	/// 注意：
	/// * none
	/// </summary>
	public static class UnityColorLib 
	{
		#region stuff for exporting to lua
		public static string LIB_NAME = "UnityColorLib";
		#endregion

		#region static variables
		public static int lf_black (ILuaState luaState)
		{
			LuaTools.L_PushUserData (luaState, Color.black, LIB_NAME);
			return 1;
		}

		public static int lf_blue (ILuaState luaState)
		{
			LuaTools.L_PushUserData (luaState, Color.blue, LIB_NAME);
			return 1;
		}

		public static int lf_clear (ILuaState luaState)
		{
			LuaTools.L_PushUserData (luaState, Color.clear, LIB_NAME);
			return 1;
		}

		public static int lf_cyan (ILuaState luaState)
		{
			LuaTools.L_PushUserData (luaState, Color.cyan, LIB_NAME);
			return 1;
		}

		public static int lf_gray (ILuaState luaState)
		{
			LuaTools.L_PushUserData (luaState, Color.gray, LIB_NAME);
			return 1;
		}

		public static int lf_green (ILuaState luaState)
		{
			LuaTools.L_PushUserData (luaState, Color.green, LIB_NAME);
			return 1;
		}

		public static int lf_magenta (ILuaState luaState)
		{
			LuaTools.L_PushUserData (luaState, Color.magenta, LIB_NAME);
			return 1;
		}

		public static int lf_red (ILuaState luaState)
		{
			LuaTools.L_PushUserData (luaState, Color.red, LIB_NAME);
			return 1;
		}

		public static int lf_white (ILuaState luaState)
		{
			LuaTools.L_PushUserData (luaState, Color.white, LIB_NAME);
			return 1;
		}

		public static int lf_yellow (ILuaState luaState)
		{
			LuaTools.L_PushUserData (luaState, Color.yellow, LIB_NAME);
			return 1;
		}
		#endregion

		#region variables
		public static int mf_GetA (ILuaState luaState)
		{
			Color c = (Color)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushNumber (c.a);
			return 1;
		}

		public static int mf_SetA (ILuaState luaState)
		{
			Color c = (Color)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			c.a =  (float)luaState.L_CheckNumber (2);
			LuaTools.L_PushUserData (luaState, c, LIB_NAME);
			return 1;
		}

		public static int mf_GetB (ILuaState luaState)
		{
			Color c = (Color)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushNumber (c.b);
			return 1;
		}

		public static int mf_SetB (ILuaState luaState)
		{
			Color c = (Color)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			c.b =  (float)luaState.L_CheckNumber (2);
			LuaTools.L_PushUserData (luaState, c, LIB_NAME);
			return 1;
		}

		public static int mf_GetG (ILuaState luaState)
		{
			Color c = (Color)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushNumber (c.g);
			return 1;
		}

		public static int mf_SetG (ILuaState luaState)
		{
			Color c = (Color)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			c.g =  (float)luaState.L_CheckNumber (2);
			LuaTools.L_PushUserData (luaState, c, LIB_NAME);
			return 1;
		}

		public static int mf_GetR (ILuaState luaState)
		{
			Color c = (Color)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushNumber (c.r);
			return 1;
		}

		public static int mf_SetR (ILuaState luaState)
		{
			Color c = (Color)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			c.r =  (float)luaState.L_CheckNumber (2);
			LuaTools.L_PushUserData (luaState, c, LIB_NAME);
			return 1;
		}
		#endregion

		#region constructors
		public static int lf_NewColor (ILuaState luaState)
		{
			float r = (float)luaState.L_CheckNumber (1);
			float g = (float)luaState.L_CheckNumber (2);
			float b = (float)luaState.L_CheckNumber (3);
			float a = (float)luaState.L_CheckNumber (4);
			LuaTools.L_PushUserData (luaState, new Color (r, g, b, a), LIB_NAME);
			return 1;
		}
		#endregion

		#region functions
		public static int mf___tostring (ILuaState luaState)
		{
			Color c = (Color)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			luaState.PushString (string.Format ("[a={0}, r={1}, g={2}, b={3}]", c.a, c.r, c.g, c.b));
			return 1;
		}
		#endregion

		#region static functions
		public static int lf_Lerp (ILuaState luaState)
		{
			Color a = (Color)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			Color b = (Color)LuaTools.L_CheckUData (luaState, 2, LIB_NAME);
			float t = (float)luaState.L_CheckNumber (3);
			LuaTools.L_PushUserData (luaState, Color.Lerp (a, b, t), LIB_NAME);
			return 1;
		}
		#endregion

		#region operators
		public static int mf___add (ILuaState luaState)
		{
			Color a = (Color)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			Color b = (Color)LuaTools.L_CheckUData (luaState, 2, LIB_NAME);
			LuaTools.L_PushUserData (luaState, a + b, LIB_NAME);
			return 1;
		}

		public static int mf___sub (ILuaState luaState)
		{
			Color a = (Color)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			Color b = (Color)LuaTools.L_CheckUData (luaState, 2, LIB_NAME);
			LuaTools.L_PushUserData (luaState, a - b, LIB_NAME);
			return 1;
		}

		public static int mf___mul (ILuaState luaState)
		{
			Color a = (Color)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			Color b = (Color)LuaTools.L_CheckUData (luaState, 2, LIB_NAME);
			LuaTools.L_PushUserData (luaState, a * b, LIB_NAME);
			return 1;
		}

		public static int mf___div (ILuaState luaState)
		{
			Color a = (Color)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			float b = (float)luaState.L_CheckNumber (2);
			LuaTools.L_PushUserData (luaState, a / b, LIB_NAME);
			return 1;
		}
		#endregion
	}
}
